/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/cupertino.dart';

/// 控制显隐，并保持占位
class HideContainer extends StatefulWidget {
  final Widget child;

  const HideContainer({super.key, required this.child});

  @override
  State<HideContainer> createState() => HideContainerState();
}

class HideContainerState extends State<HideContainer> {
  Size? _placeholderSize;

  set placeholderSize(Size? value) {
    if (_placeholderSize == value) {
      return;
    }
    setState(() {
      _placeholderSize = value;
    });
  }

  Size? get placeholderSize => _placeholderSize;

  bool _visible = true;

  set isVisible(bool value) {
    if (_visible == value) {
      return;
    }
    setState(() {
      _visible = value;
    });
  }

  bool get isVisible => _visible;

  bool get isInTree => _placeholderSize == null;

  @override
  Widget build(BuildContext context) {
    if (_placeholderSize != null) {
      return SizedBox.fromSize(
        size: _placeholderSize,
      );
    }
    return Visibility(
        visible: _visible,
        maintainSize: true,
        maintainState: true,
        maintainAnimation: true,
        child: widget.child);
  }
}
